package com.abing.starry.controller;

import com.abing.api.model.StarryApiRequest;
import com.abing.starry.annotation.AuthCheck;
import com.abing.starry.common.BaseController;
import com.abing.starry.common.BaseResponse;
import com.abing.starry.common.ResultUtils;
import com.abing.starry.model.entity.InterfaceInfoEntity;
import com.abing.starry.model.request.interfaces.QueryCountRequest;
import com.abing.starry.model.request.interfaces.QueryInterfaceRequest;
import com.abing.starry.model.vo.InterfaceInfoVO;
import com.abing.starry.service.InterfaceInfoService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ContentDisposition;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Author CaptainBing
 * @Date 2024/8/23 14:42
 * @Description 接口信息
 */
@RestController
@RequestMapping("/interface")
@RequiredArgsConstructor
public class InterfaceInfoController implements BaseController<InterfaceInfoEntity> {


    private final InterfaceInfoService interfaceInfoService;

    @Override
    public IService<InterfaceInfoEntity> getService() {
        return interfaceInfoService;
    }

    @PostMapping("/invoke")
    @Operation(summary = "Api接口调用")
    public String apiInvoke(@RequestBody StarryApiRequest starryApiRequest) {
        return interfaceInfoService.apiInvoke(starryApiRequest);
    }

    @PostMapping("/query/count/page")
    @Operation(summary = "分页查询接口调用情况")
    public IPage<InterfaceInfoVO> queryInvokeCountByPage(@RequestBody QueryCountRequest queryCountRequest) {
        return interfaceInfoService.queryInvokeCountByPage(queryCountRequest);
    }

    @PostMapping("/query/page")
    @Operation(summary = "分页查询接口")
    public IPage<InterfaceInfoEntity> queryInterfaceByPage(@RequestBody QueryInterfaceRequest queryInterfaceRequest) {
        return interfaceInfoService.queryInterfaceByPage(queryInterfaceRequest);
    }

    @GetMapping("/apply/{id}/{isAgree}")
    @Operation(summary = "申请接口")
    public boolean applyInterface(@PathVariable String id,@PathVariable Boolean isAgree) {
        return interfaceInfoService.applyInterface(id,isAgree);
    }

    @GetMapping("/query/{id}")
    @Operation(summary = "根据Id查询接口")
    public InterfaceInfoEntity queryInterfaceById(@PathVariable String id) {
        return interfaceInfoService.getById(id);
    }

    @PostMapping("/save")
    @Operation(summary = "新增接口")
    public boolean saveInterface(@RequestBody InterfaceInfoEntity interfaceInfoEntity) {
        return interfaceInfoService.saveInterface(interfaceInfoEntity);
    }

    @GetMapping("/export")
    @AuthCheck(mustRole = "admin")
    @Operation(summary = "导出所有接口的信息")
    public void exportInterface(HttpServletResponse response) {

        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader("Content-Disposition",ContentDisposition.attachment()
                                                                   .filename("ddd.xlsx", StandardCharsets.UTF_8)
                                                                   .build()
                                                                   .toString());

        try {
            EasyExcel.write(response.getOutputStream())
                     .excelType(ExcelTypeEnum.XLSX)
                     .sheet("接口信息")
                     .head(InterfaceInfoEntity.class)
                     .doWrite(interfaceInfoService.list());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    @GetMapping("_init")
    public BaseResponse<?> init() {
        return ResultUtils.success(interfaceInfoService.list());
    }
}
